package com.avast.personalbudgetapp.presenter.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.avast.personalbudgetapp.R;
import com.avast.personalbudgetapp.global.constant.Constants;
import com.avast.personalbudgetapp.model.service.BudgetAppService;
import com.avast.personalbudgetapp.model.service.BudgetAppServiceImpl;

public abstract class AbstractActivity extends ActionBarActivity {
	
	protected int menuRes;
	protected static int layoutRes;
	
	
	protected BudgetAppService appService;
	
	public AbstractActivity(int layoutRes, int menuRes){	
		
		this.layoutRes = layoutRes;
		this.menuRes = menuRes;
		
		allowThreads();
		appService = BudgetAppServiceImpl.getInstance();
		
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(menuRes, menu);
		return true;
	}	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_sync:
			appService.syncData();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}	
	
	@SuppressLint("NewApi")
	private void allowThreads(){
		//TODO Use AsyncTask to run the accessing thread background, instead of permitting all long running threads		
		//Allowing long running threads especially for network access
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);	
	}
	
	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(layoutRes, container,
					false);
			return rootView;
		}
	}
	
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
		return;
	}	
	
	/**
	 * Initializes the activity with view objects
	 */
	protected void init(){
		//Override this method for each Activity
	}
	
	/**
	 * Used by actions
	 * @param view
	 */
	public void addTransaction(View view){
		addTransaction();
	}
	
	protected void addTransaction(){
        Intent intent = new Intent(this, TransactionAddOrEditActivity.class);
        intent.putExtra(Constants.INTENT_TRANSACTION_EDIT_MODE, Constants.TRANSACTION_ADD);
        startActivity(intent);
	}	
	
	/**
	 * Used by actions
	 * @param view
	 */
	public void showTransactions(View view){
		startTransactionListActivity();
	}	
	
	protected void startTransactionListActivity(){
		Intent intent = new Intent(getApplicationContext(), TransactionListActivity.class);
		startActivity(intent);
	}
	
	//TODO Refresh UI correctly
	protected void refresh(){
		ListView listView = new ListView(this);
		listView.invalidate();
	}
	
	protected void goHome(){
		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
	}	
	
	public void sync(View view){
		appService.syncData();
	}	

}
